package com.kaiyidiannao.activity.login_or_registered;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.AppUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.data.Validate;
import com.kaiyidiannao.main.MainActivity;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.GetLogin;
import com.kaiyidiannao.model.req.model.GetUserDetail;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by wzg on 14-4-14.
 */
public class UserLogin extends BaseActionBarActivity implements Observer {

    private TextView forgetPassword;
    private ImageButton loginBt;
    private EditText login_telephone;
    private EditText login_password;
    String telephone;
    String password;
    String classname;
    Class c;
    int tip = 1;
    SharedPreferences sharedPreferences;

    private ArrayList<UserBean> userBeans = DataCenter.userLogin;
    private ArrayList<UserBean> userBeanList = DataCenter.userBeans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_login);

        AppUtils.setupUI(findViewById(R.id.login), this);
//        Intent intent = getIntent();
//        if (intent.getStringExtra("ClassName") != null) {
//            classname = intent.getStringExtra("ClassName");
//            try {
//                c = Class.forName(classname);
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//        }


//        Log.d("name", classname+"---------------------");

        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        init();
    }

    public void init() {

        forgetPassword = (TextView) findViewById(R.id.forgetPassword);
        forgetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                forgetPasswordClick();
            }
        });
        loginBt = (ImageButton) findViewById(R.id.login_bt);
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginBtClick();
            }
        });

        login_telephone = (EditText) findViewById(R.id.login_telephone);
        login_password = (EditText) findViewById(R.id.login_password);

        sharedPreferences = getSharedPreferences("user_detail", Context.MODE_PRIVATE);
        Log.d("mobile", sharedPreferences.getString("mobile", ""));
        String mobile = sharedPreferences.getString("mobile", "");
        if (mobile == null){

        }else{
            login_telephone.setText(mobile);
        }
    }

    public void forgetPasswordClick() {
        Intent intent = new Intent(this, ForgetPassword.class);
        startActivity(intent);
    }

    public void loginBtClick() {
        telephone = login_telephone.getText().toString();
        password = login_password.getText().toString();
        if (telephone != null && !"".equals(telephone)) {
            if (telephone.length() == 11) {
                if (login_telephone.getText().toString().matches(Validate.phoneValidate)) {
                    if (password != null && !"".equals(password)) {
                        if (password.length() <= 20 && password.length() >= 6) {
                            if (login_password.getText().toString().matches(Validate.passwordValidate)) {
                                showProgressDialog("正在登录");
                                new GetLogin(telephone, password, "1").execute();
                            } else {
                                AlertDialog builder = new AlertDialog.Builder(UserLogin.this).setTitle("错误信息")
                                        .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                            }
                        } else {
                            AlertDialog builder = new AlertDialog.Builder(UserLogin.this).setTitle("错误信息")
                                    .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                        }
                    } else {
                        AlertDialog builder = new AlertDialog.Builder(UserLogin.this).setTitle("错误信息")
                                .setMessage("密码不能为空").setPositiveButton("确定", null).show();
                    }

                } else {
                    AlertDialog builder = new AlertDialog.Builder(UserLogin.this).setTitle("错误信息")
                            .setMessage("手机号为11位，只能输入数字，不能输入空格").setPositiveButton("确定", null).show();
                }

            } else {
                AlertDialog builder = new AlertDialog.Builder(UserLogin.this).setTitle("错误信息")
                        .setMessage("手机号为11位，只能输入数字，不能输入空格").setPositiveButton("确定", null).show();
            }

        } else {
            AlertDialog builder = new AlertDialog.Builder(UserLogin.this).setTitle("错误信息")
                    .setMessage("手机号不能为空").setPositiveButton("确定", null).show();
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        closeProgressDialog();

        String code = DataCenter.code;
        if (flag == DataCenter.flag.GET_LOGIN_SUCCESS
                || flag == DataCenter.flag.GET_LOGIN_FAILED) { //类表数据
            if (flag == DataCenter.flag.GET_LOGIN_SUCCESS) {
                if (code.equals(DataCenter.successFlag)) {
                    DataCenter.password = password;
                    closeProgressDialog();
//                    if (c == null) {
//                        Log.d("ccccccccc", "+z+++++++++++++++");
//                        new GetUserDetail(userBeans.get(0).getUserid()).execute();
//                    } else {
//                        tip = 0;
//                        new GetUserDetail(userBeans.get(0).getUserid()).execute();
//                        Intent intent = new Intent(UserLogin.this, c);
//                        startActivity(intent);
//                        finish();
//                    }
                    new GetUserDetail(userBeans.get(0).getUserid()).execute();

                    if (DataCenter.msg != null) {
                        Toast.makeText(UserLogin.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(UserLogin.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                }
            } else {
//                Toast.makeText(UserLogin.this, "网络连接错误，请稍后重试", Toast.LENGTH_SHORT).show();
            }

            if (flag == DataCenter.flag.GET_LOGIN_FAILED) {
                if (code.equals(DataCenter.failedFlag)) {
                    closeProgressDialog();
                    // Toast.makeText(UserLogin.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                }
            }
        }
        if (flag == DataCenter.flag.GET_USER_DETAIL_SUCCESS) {
//            if (tip == 0) {
//                tip = 1;
//                saveUserDetail();
//                finish();
//            } else {
//                Intent intent = new Intent(UserLogin.this, MainActivity.class);
//                intent.putExtra("flag", "flag");
//                startActivity(intent);
                finish();
                saveUserDetail();
//            }
        }

    }

    public void saveUserDetail() {
        sharedPreferences = getSharedPreferences("user_detail", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        // editor.putString("userid", userBeans.get(0).getUserid());
        editor.putString("mobile", userBeanList.get(0).getMobile());
//        editor.putString("address", userBeanList.get(0).getAddress());
//        editor.putString("username", userBeanList.get(0).getUsername());
//        editor.putString("truename", userBeanList.get(0).getTruename());
//        if (!TextUtils.isEmpty(password)) {
//            Log.d("password", password);
//            editor.putString("password", password);
//        }
        //   editor.putString("qq", userBeanList.get(0).getQq());
        editor.commit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, UserLogin.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

}
